<?php
class categoryMod extends commonMod
{
	public function __construct()
	{
		parent::__construct();

	}
	//分类首页
	public function index()
	{		
		$this->assign('cat',$this->getCat());
		$this->display();
	}
	
	//添加外部链接
	public function linkadd(){
		//显示添加分类页面
		if(empty($_POST['do']))
		{ 
			$pid=intval($_GET[0]);
			$this->assign('pid',$pid);
			$this->assign('cat',$this->getCat());//获取格式化后的分类树			
			$this->display();
			return;
		}
		$data=array();
		$data['name']=in($_POST['linktitle']);
		$data['url']=in($_POST['linkurl']);
		$data['isshow']=in($_POST['show']);
		$data['type']=3;
		$data['addtime']=time();
		
		//验证数据
		if(empty($data['name']))
		{
			$this->error('链接名称不能为空');
		}
		if(empty($data['url']))
		{
			$this->error('链接地址不能为空');
		}

		
		//数据库操作
		if($this->model->table('category')->data($data)->insert())
		
			$this->success('链接添加成功');
		else
			$this->error('链接添加失败');
	}
	//编辑外部链接
	public function linkedit(){
			//没有提交数据，显示编辑页面
		if(empty($_POST['do']))
		{
			$id=intval($_GET[0]);
			if(empty($id))
			{
				$this->error('参数传递错误');
			}		
			$condition['cid']=$id;
			$info=$this->model->table('category')->where($condition)->find();//获取当前分类信息
			if(empty($info))
			{
				$this->error('该分类不存在或者已被删除');
			}
			$this->assign('info',$info);
			$this->assign('cat',$this->getCat());//获取格式化后的分类树
			$this->display();
			return;
		}
		$data=array();
		$data['cid']=in($_POST['id']);
		$data['name']=in($_POST['linktitle']);
		$data['url']=in($_POST['linkurl']);
		$data['isshow']=in($_POST['show']);
		$data['edittime']=time();
		//验证数据
		if(empty($data['name']))
		{
			$this->error('栏目名称不能为空');
		}

		$condition['cid']=$data['cid'];
		//数据库操作
		if($this->model->table('category')->data($data)->where($condition)->update()){
			
			$this->success('链接更新成功!');
		}
		else{
			$this->error('链接更新失败!');
		}
	}
	//删除外部链接
	public function linkdel(){
		$id=intval($_GET[0]);
		if(empty($id))
		{
			$this->error('参数传递错误');
		}
		$condition=array();
		//检测子栏目是否存在
		$condition['pid']=$id;
		if($this->model->table('category')->where($condition)->count())
		{
			$this->error('请先删除该栏目下面的子栏目');	
		}
		unset($condition);//将上一次查询条件清空
		$condition['cid']=$id;
		if($this->model->table('category')->where($condition)->delete())
		
			$this->success('删除链接成功!');//删除成功后跳转到分类首页
		else
			$this->error('删除链接失败!');
	}
	
	//添加单页模块
	public function pageadd(){
		//显示添加分类页面
		if(empty($_POST['do']))
		{ 
		 	$mid = intval($_GET[0]); //模型ID获取
            require (SEOPATH . '/config.php');
            // 列出模板文件
            $tpl_dir = opendir(SEOPATH . "/" . $config['TPL_TEMPLATE_PATH']);
			
            // 列出模板文件
            $tpl_dir2 = opendir(SEOPATH . "/" . $config['TPL_TEMPLATE_PATH']);
			
			$pid=intval($_GET[0]);
			$this->assign('pid',$pid);
			$this->assign('tpl_dir',$tpl_dir);
			$this->assign('tpl_dir2',$tpl_dir2);
			$this->assign('cat',$this->getCat());//获取格式化后的分类树			
			$this->display();
			// 关闭模板文件
            closedir($tpl_dir);
            closedir($tpl_dir2);
            return;
		}
		//获取数据
		include(CP_PATH.'ext/Pinyin.class.php');//导入Pinyin.class.php类
		$py=new Pinyin();
		$insert=array();
		$insert['pid']=intval($_POST['pid']);//上一级分
		$insert['name']=in($_POST['pagetitle']);//分类名次类id
		$insert['enname']=$py->output($_POST['pagetitle']);
		$insert['keyword']=html_in($_POST['keyword']);//分类描述
		$insert['tpl_category']=in($_POST['tpl']);//模版
		$insert['type']=2;
		$insert['addtime']=time();
		
		//验证数据
		if(empty($insert['name']))
		{
			$this->error('单页名称不能为空！');
		}
		
		$id=$this->model->table('category')->data($insert)->insert();
		$date['cid']=$id;
		$date['content']=html_in($_POST['content']);
		
		
		if(empty($date['content']))
		{
			$this->error('请写入您要发表的内容！');
		}
		
		if($this->model->table('page')->data($date)->insert())
		
			$this->success('添加单页内容成功');
		else
			$this->error('添加单页内容失败');
		
	}
	
	//编辑单页模块
	public function pageedit(){
		//没有提交数据，显示编辑页面
		if(empty($_POST['do']))
		{
            require (SEOPATH . '/config.php');
            // 列出模板文件
            $tpl_dir = opendir(SEOPATH . "/" . $config['TPL_TEMPLATE_PATH']);
			
            // 列出模板文件
            $tpl_dir2 = opendir(SEOPATH . "/" . $config['TPL_TEMPLATE_PATH']);
			
			$id=intval($_GET[0]);
			if(empty($id))
			{
				$this->error('参数传递错误');
			}
			//$info=$this->model->table('category')->where($condition)->find();//获取当前分类信息
			$sql="SELECT * FROM {$this->model->pre}category LEFT JOIN {$this->model->pre}page ON {$this->model->pre}category.cid = {$this->model->pre}page.cid where {$this->model->pre}category.cid=$id";
			$info=$this->model->query($sql);//执行查询
			if(empty($info))
			{
				$this->error('该分类不存在或者已被删除');
			}
			$pid=$info['pid'];
			$this->assign('info',$info);
			$this->assign('pid',$pid);
			$this->assign('tpl_dir',$tpl_dir);
			$this->assign('tpl_dir2',$tpl_dir2);
			$this->assign('cat',$this->getCat());//获取格式化后的分类树
			$this->display();
			
			// 关闭模板文件
            closedir($tpl_dir);
            closedir($tpl_dir2);
			return;
		}
		
		$data=array();
		$data['cid']=in($_POST['id']);
		$data['pid']=in($_POST['pid']);
		$data['name']=in($_POST['pagetitle']);
		$data['enname']=in($_POST['entitle']);
		$data['keyword']=in($_POST['keyword']);
		$data['tpl_category']=in($_POST['tpl']);
		$data['isshow']=in($_POST['show']);
		$data['edittime']=time();
		//验证数据
		if(empty($data['name']))
		{
			$this->error('栏目名称不能为空');
		}

		$condition['cid']=$data['cid'];
		$id=$this->model->table('category')->data($data)->where($condition)->update();
		
		$id=$data['cid'];
		$update['content']=html_in($_POST['content']);
		//数据库操作
		if($this->model->table('page')->data($update)->where('cid='.$id)->update()){
			
			$this->success('内容更新成功!');
		}
		else{
			$this->error('内容更新失败!');
		}
	}
	//删除单页模块
	public function pagedel(){
		$id = intval($_GET[0]); //获取传递信息
        if (empty($id)) {
            $this->error('参数传递错误！');
            return;
        }
		$condition=array();
		//检测子栏目是否存在
		$condition['pid']=$id;
		if($this->model->table('category')->where($condition)->count())
		{
			$this->error('请先删除该栏目下面的子栏目!');	
		}
		unset($condition);//将上一次查询条件清空
		
		$condition['cid']=$id;
		$this->model->table('category')->where($condition)->delete(); //删除栏目
		$condition2['cid']=$id;	
		if( $this->model->table('page')->where($condition2)->delete()){
			
			$this->success('删除模块成功!');	//删除栏目文章信息
			
		}else{
			 $this->error('删除模块失败！');
		}
	}
	//添加新闻模块
	public function listadd(){
		//显示添加分类页面
		if(empty($_POST['do']))
		{ 
		 	$mid = intval($_GET[0]); //模型ID获取
            require (SEOPATH . '/config.php');
            // 列出模板文件
            $tpl_dir = opendir(SEOPATH . "/" . $config['TPL_TEMPLATE_PATH']);
			
            // 列出模板文件
            $tpl_dir2 = opendir(SEOPATH . "/" . $config['TPL_TEMPLATE_PATH']);
			
			$pid=intval($_GET[0]);
			$this->assign('pid',$pid);
			$this->assign('tpl_dir',$tpl_dir);
			$this->assign('tpl_dir2',$tpl_dir2);
			$this->assign('cat',$this->getCat());//获取格式化后的分类树			
			$this->display();
			// 关闭模板文件
            closedir($tpl_dir);
            closedir($tpl_dir2);
            return;
		}
		//获取数据
		include(CP_PATH.'ext/Pinyin.class.php');//导入Pinyin.class.php类
		$py=new Pinyin();
		$insert=array();
		$insert['pid']=intval($_POST['pid']);//
		$insert['name']=in($_POST['title']);//分类名次类id
		$insert['enname']=$py->output($_POST['title']);
		$insert['keyword']=html_in($_POST['keyword']);//分类关键词	
		$insert['description']=html_in($_POST['description']);//分类描述
		$insert['tpl_category']=in($_POST['tpl_category']);//模版
		$insert['tpl_show']=in($_POST['tpl_content']);//模版
		$update['isshow']=in($_POST['show']);//前台显示
		$insert['type']=1;
		$insert['addtime']=time();
		
		
		//验证数据
		if(empty($insert['name']))
		{
			$this->error('栏目名称不能为空！');
		}
		if(empty($insert['tpl_category']))
		{
			$this->error('请选择网站列表模版！');
		}
		if(empty($insert['tpl_show']))
		{
			$this->error('请选择网站内容模版！');
		}
		
		if($id=$this->model->table('category')->data($insert)->insert())
		
			$this->success('添加列表栏目成功！');
		else
			$this->error('添加列表栏目失败！');
		
	}
	//编辑列表模块
	public function listedit(){
		//显示添加分类页面
		if(empty($_POST['do']))
		{ 
            require (SEOPATH . '/config.php');
            // 列出模板文件
            $tpl_dir = opendir(SEOPATH . "/" . $config['TPL_TEMPLATE_PATH']);
            // 列出模板文件
            $tpl_dir2 = opendir(SEOPATH . "/" . $config['TPL_TEMPLATE_PATH']);
			
			$id=intval($_GET[0]);
			if(empty($id))
			{
				$this->error('参数传递错误');
			}
			$condition['cid']=$id;
			$info=$this->model->table('category')->where($condition)->find();//获取当前分类信息
			$pid=$info['pid'];
			$this->assign('pid',$pid);
			$this->assign('info',$info);
			$this->assign('tpl_dir',$tpl_dir);
			$this->assign('tpl_dir2',$tpl_dir2);
			$this->assign('cat',$this->getCat());//获取格式化后的分类树			
			$this->display();
			// 关闭模板文件
            closedir($tpl_dir);
            closedir($tpl_dir2);
            return;
		}
		//获取数据
		include(CP_PATH.'ext/Pinyin.class.php');//导入Pinyin.class.php类
		$py=new Pinyin();
		$update=array();
		$update['cid']=intval($_POST['cid']); 
		$update['pid']=intval($_POST['pid']); 
		$update['name']=in($_POST['title']);//分类名次类id
		$update['enname']=$py->output($_POST['title']);
		$update['keyword']=html_in($_POST['keyword']);//分类关键词	
		$update['description']=html_in($_POST['description']);//分类描述
		$update['tpl_category']=in($_POST['tpl_category']);//模版
		$update['tpl_show']=in($_POST['tpl_content']);//模版
		$update['isshow']=in($_POST['show']);//前台显示
		$update['type']=1;
		$update['edittime']=time();
		
		$condition['cid']=$update['cid'];
		if($this->model->table('category')->data($update)->where($condition)->update()){
			
			$this->success('列表栏目更新成功!');
		}
		else{
			$this->error('列表栏目更新失败!');
		}
		
	}
	
	//排序
	 public function sort()
    {
      $cid=$_POST['cid'];
      $sort=$_POST['num'];
      if(empty($cid)){
        return;
      }
      $condition['cid']=$cid;
      $data['sort']=$sort;
      $this->model->table('category')->data($data)->where($condition)->update();
    }
	
	//获取分类树，$id，分类id,$id=0，获取所有分类结构树
	public function getCat($id=0)
	{
		require(CP_PATH.'lib/Category.class.php');//导入Category.class.php无限分类
		//查询分类信息
		$sql="SELECT * FROM {$this->model->pre}category LEFT JOIN {$this->model->pre}model ON {$this->model->pre}category.type = {$this->model->pre}model.mid";
		$data=$this->model->query($sql);//执行查询		
		//array('id','pid','name','cname'),字段映射，格式化后的分类名次问cname
		$cat=new Category(array('cid','pid','name','cname'));//初始化无限分类
		
		return $cat->getTree($data,$id);//获取分类数据树结构
	}
	
}
?>